﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using Common.Util;
using DevExpress.XtraEditors;
using Dewin.BO.AgentMember;
using Log.Controller;
using Tabber.Model;
using Tabber.Util;

namespace Tabber.AgentControls
{
    public partial class UcMainAgent : DevExpress.XtraEditors.XtraUserControl
    {
        #region Variables
        /// <summary>
        /// Bien de luu id cua agent
        /// </summary>
        public string AgentId = string.Empty;

        /// <summary>
        /// Doi tuong agent
        /// </summary>
        public Agent AgentObj = null;
        #endregion

        #region User Control
        public UcMainAgent()
        {
            InitializeComponent();
            SetText();
        }

        private void UcMainAgent_Load(object sender, EventArgs e)
        {
            LoadInfo();
            ResetForm();
        }

        private void btnSetting_Click(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// Load thong tin tu doi tuong agent va truyen vao form
        /// </summary>
        private void LoadInfo()
        {
            try
            {
                if (AgentObj == null) return;

                linkAddress.Text = AgentObj.Address;
                lblStatus.Text = TabberCommon.GetString(AgentObj.IsLogin ? "IsLogin" : "IsLogout");
            }
            catch (Exception ex)
            {
                LogController.WriteLog(System.Runtime.InteropServices.Marshal.GetExceptionCode(), ex, Network.GetLocalIp());
            }
        }

        /// <summary>
        /// Ham set text theo ngon ngu
        /// </summary>
        public void SetText()
        {
            try
            {
                btnLoginSetting.Text = TabberCommon.GetString("LoginSetting");
                btnStart.Text = TabberCommon.GetString("Start");
                btnStop.Text = TabberCommon.GetString("Stop");
                btnHomePage.Text = TabberCommon.GetString("HomePage");
                btnMembers.Text = TabberCommon.GetString("ListMembers");
                lblAddress.Text = TabberCommon.GetString("Address");
                groupControl1.Text = TabberCommon.GetString("AccountInfo");
                lblStatus.Text = TabberCommon.GetString(AgentObj != null && AgentObj.IsLogin ? "IsLogin" : "IsLogout");

                tabAllMemberBet.Text = TabberCommon.GetString("AllMemberBet");
                tabNewMemberBet.Text = TabberCommon.GetString("NewMemberBet");
            }
            catch (Exception ex)
            {
                LogController.WriteLog(System.Runtime.InteropServices.Marshal.GetExceptionCode(), ex, Network.GetLocalIp());
            }
        }
        #endregion

        #region Start/Stop
        /// <summary>
        /// Xu ly dung chay agent
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStop_Click(object sender, EventArgs e)
        {
            ResetForm();
        }

        /// <summary>
        /// Xu ly bat dau chay Agent
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnStart_Click(object sender, EventArgs e)
        {
            RunForm();
            AgentObj.Start();
        }

        /// <summary>
        /// Reset trang thai hien thi cua cac control khi chua chay
        /// </summary>
        private void ResetForm()
        {
            // Tat mo cac button
            btnStart.Enabled = true;
            btnStop.Enabled = false;

            // Xoa tat ca thread cua agent

            // Khoi tao cookies moi cho agent [dong nghia voi viec xoa cookies dang xai, neu co]
        }

        /// <summary>
        /// Doi trang thai hien thi cua cac control khi dang chay
        /// </summary>
        private void RunForm()
        {
            // Tat mo cac button
            btnStart.Enabled = false;
            btnStop.Enabled = true;

        }
        #endregion
    }
}
